package com.github.soiff.shadow.plugin.helper;

import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
import com.google.common.base.Strings;

import java.io.IOException;
import java.util.List;

import static com.github.soiff.shadow.plugin.utils.Constants.COMMA;

/**
 * Created by Soiff on 2016/05/20.
 *
 * @author : Soiff
 */
public class OfHelper implements Helper {
    @Override
    public CharSequence apply(Object source, Options options) throws IOException {
        if(source == null) {
            return options.inverse();
        } else {
            String _source = source.toString();
            String param = options.param(0);
            if(Strings.isNullOrEmpty(param)) {
                return options.inverse();
            } else {
                List targets = COMMA.splitToList(param);
                return targets.contains(_source)?options.fn():options.inverse();
            }
        }
    }
}
